from typing import List
from common.enum.strategy_group_enum import StrategyGroup
from common.enum.strategy_type_enum import StrategyType
from dto.strategy_info import StrategyInfoMetadata
from dto.strategy_assessment import RiskAssessment
from dto.strategy_stock_day import StrategyStockDay
from service.strategy.base_strategy import BaseStrategy


class SixDaysVolumeComparisonStrategy(BaseStrategy):
    """
    6天成交量对比策略，后三天总成交量大于前三天时计分。
    """

    def analyze(self, trade_info_list: List[StrategyStockDay]) -> RiskAssessment:
        """
        分析6天的成交量对比，计算评分。
        """
        node_point = 0
        description = ""

        # 确保至少有6天的交易数据
        if len(trade_info_list) >= 6:
            # 计算前三天和后三天的成交量总和
            volume_first_three_days = sum(day.vol for day in trade_info_list[:3])
            volume_last_three_days = sum(day.vol for day in trade_info_list[3:6])

            if volume_last_three_days > volume_first_three_days:
                node_point += 1  # 后三天总量大于前三天，加1分
                increase_percentage = (
                    (volume_last_three_days - volume_first_three_days)
                    / volume_first_three_days
                ) * 100

                description += "后三天的成交量总和大于前三天总和，得1分。"

                # 每多5%，多加2分
                additional_points = int(increase_percentage // 5) * 2
                node_point += additional_points
                description += f" 成交量增加了{increase_percentage:.2f}%，额外得{additional_points}分。"

        return RiskAssessment(
            stock_code=trade_info_list[0].stock_code,
            description=description,
            config=self.strategyConfig(),
            node_point=node_point,
        )

    def strategyConfig(self) -> StrategyInfoMetadata:
        """
        返回策略的配置
        """
        return StrategyInfoMetadata(
            strategy_code="six_days_volume_comparison",
            strategy_name="6天成交量对比",
            strategy_group=StrategyGroup.ATTENTION,
            strategy_type=StrategyType.VOLUME,
            analysis_day=6,
            strategy_level=3,
        )
